﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class WebControls_DatePicker : System.Web.UI.UserControl
{

    public DateTime SelectedDate;

    public DateTime getSelectedDate
    {
        get
        {
            return getDateTime();
            //return new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDay.SelectedValue));
        }
    }

    public Boolean IsRequired
    {
        set
        {
            rfvDateTime.Enabled = value;
            cfvDateTime.Enabled = value;
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SelectedDate != DateTime.MinValue)
                txtDateTime.Text = SelectedDate.ToString("dd/MM/yyyy");
            imgCalendar.Attributes.Add("onclick", "return OpenShowDialog('" + "/" + "popup/dtpicker.aspx?textid=" + txtDateTime.ClientID + "',200,237);");
        }
    }
    private DateTime getDateTime()
    {
        try
        {
            String[] dts = txtDateTime.Text.Split('/');
            DateTime dt = new DateTime(Convert.ToInt32(dts[2]), Convert.ToInt32(dts[1]), Convert.ToInt32(dts[0]));
            return dt;
        }
        catch
        {
            return DateTime.MinValue;
        }
    }

    private Boolean IsValidDate()
    {
        try
        {
            String[] dts = txtDateTime.Text.Split('/');
            DateTime dt = new DateTime(Convert.ToInt32(dts[2]), Convert.ToInt32(dts[1]), Convert.ToInt32(dts[0]));
            return true;
        }
        catch
        {
            return false;
        }
    }

    public RequiredFieldValidator ReqValidator
    {
        get { return rfvDateTime; }
    }
    public CustomValidator CustValidator
    {
        get { return cfvDateTime; }
    }

    /*
        private void LoadDDLs()
        {
            for (int i = 1; i <= 31; i++)
            {
                ddlDay.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            ddlDay.SelectedValue = SelectedDate.Day.ToString();
            for (int i = 1; i <= 12; i++)
            {
                //ddlMonth.Items.Add(new ListItem((new DateTime(DateTime.Now.Year, i, 1)).ToString("MMMM"), i.ToString()));
                ddlMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            ddlMonth.SelectedValue = SelectedDate.Month.ToString();
            for (int i = 2000; i <= DateTime.Now.Year + 5; i++)
            {
                ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            ddlYear.SelectedValue = SelectedDate.Year.ToString();
        }
     */
    protected void cfvDateTime_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (!String.IsNullOrEmpty(txtDateTime.Text.Trim()))
        {
            if (!IsValidDate())
                args.IsValid = false;
        }
    }
    protected void imgCalendar_PreRender(object sender, EventArgs e)
    {

    }
}
